package com.alison.tableapisql.chapter2_timeAttr;

import com.alison.tableapisql.chapter1_tableapiandsql.model.SensorReading;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.timestamps.BoundedOutOfOrdernessTimestampExtractor;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.table.api.Over;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.Tumble;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.types.Row;

public class E4_TableTest5_TimeAndWindow {
    public static void main(String[] args) throws Exception {
        // 1. 创建环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);
        String filePath = "D:\\workspace\\lab\\learnbigdata\\learnflink\\flink-datastream\\src\\main\\resources\\tableapi\\E1.txt";

        // 2. 读入文件数据，得到DataStream
        DataStream<String> inputStream = env.readTextFile(filePath);

        // 3. 转换成POJO
        DataStream<SensorReading> dataStream = inputStream.map(line -> {
                    String[] fields = line.split(",");
                    return new SensorReading(fields[0], new Long(fields[1]), new Double(fields[2]));
                })
                .assignTimestampsAndWatermarks(new BoundedOutOfOrdernessTimestampExtractor<SensorReading>(Time.seconds(2)) {
                    @Override
                    public long extractTimestamp(SensorReading element) {
                        return element.getTimestamp() * 1000L;
                    }
                });

        // 4. 将流转换成表，定义时间特性
        //        Table dataTable = tableEnv.fromDataStream(dataStream, "id, timestamp as ts, temperature as temp, pt.proctime");
        Table dataTable = tableEnv.fromDataStream(dataStream, "id, timestamp as ts, temperature as temp, rt.rowtime");

        //        dataTable.printSchema();
        //
        //        tableEnv.toAppendStream(dataTable,Row.class).print();

        tableEnv.createTemporaryView("sensor", dataTable);

        // 5. 窗口操作
        // 5.1 Group Window
        // table API
        Table resultTable = dataTable.window(Tumble.over("10.seconds").on("rt").as("tw"))
                .groupBy("id, tw")
                .select("id, id.count, temp.avg, tw.end");

        // SQL
        Table resultSqlTable = tableEnv.sqlQuery("select id, count(id) as cnt, avg(temp) as avgTemp, tumble_end(rt, interval '10' second) " +
                "from sensor group by id, tumble(rt, interval '10' second)");

        // 5.2 Over Window
        // table API
        Table overResult = dataTable.window(Over.partitionBy("id").orderBy("rt").preceding("2.rows").as("ow"))
                .select("id, rt, id.count over ow, temp.avg over ow");

        // SQL
        Table overSqlResult = tableEnv.sqlQuery("select id, rt, count(id) over ow, avg(temp) over ow " +
                " from sensor " +
                " window ow as (partition by id order by rt rows between 2 preceding and current row)");

        //        dataTable.printSchema();
        //        tableEnv.toAppendStream(resultTable, Row.class).print("result");
        //        tableEnv.toRetractStream(resultSqlTable, Row.class).print("sql");
        tableEnv.toAppendStream(overResult, Row.class).print("result");
        tableEnv.toRetractStream(overSqlResult, Row.class).print("sql");

        env.execute();
    }
    /*
    因为partition by id order by rt rows between 2 preceding and current row，所以最后2次关于sensor_1的输出的count(id)都是3,但是计算出来的平均值不一样。（前者计算倒数3条sensor_1的数据，后者计算最后最新的3条sensor_1数据的平均值）


sql> (true,+I[sensor_1, 2019-01-17T09:43:19, 1, 35.8])
result> +I[sensor_1, 2019-01-17T09:43:19, 1, 35.8]
sql> (true,+I[sensor_6, 2019-01-17T09:43:21, 1, 15.4])
result> +I[sensor_6, 2019-01-17T09:43:21, 1, 15.4]
sql> (true,+I[sensor_7, 2019-01-17T09:43:22, 1, 6.7])
result> +I[sensor_7, 2019-01-17T09:43:22, 1, 6.7]
result> +I[sensor_10, 2019-01-17T09:43:25, 1, 38.1]
sql> (true,+I[sensor_10, 2019-01-17T09:43:25, 1, 38.1])
result> +I[sensor_1, 2019-01-17T09:43:27, 2, 36.05]
sql> (true,+I[sensor_1, 2019-01-17T09:43:27, 2, 36.05])
result> +I[sensor_1, 2019-01-17T09:43:29, 3, 34.96666666666666]
sql> (true,+I[sensor_1, 2019-01-17T09:43:29, 3, 34.96666666666666])
sql> (true,+I[sensor_1, 2019-01-17T09:43:32, 3, 35.4])
result> +I[sensor_1, 2019-01-17T09:43:32, 3, 35.4]
     */
}